<?php

if (!isset($_GET['name']) || $_GET['name'] == "") die();

if ($_GET['action'] == "create"){
	posix_mkfifo("/tmp/schackofdoompipes/" . $_GET['name'], 0777);
	die();
}

if (file_exists("/tmp/schackofdoompipes" . $_GET['name'])===false){
	posix_mkfifo("/tmp/schackofdoompipes/" . $_GET['name'], 0777);
}

if ($_GET['action'] == "send"){
	$pipe = fopen("/tmp/schackofdoompipes/" . $_GET['name'], "w");
	fwrite($pipe, $_GET['message']);
	fclose($pipe);
} else if ($_GET['action'] == "receive"){
	for(;;){
		$msg = file_get_contents("/tmp/schackofdoompipes/" . $_GET['name']);
		echo str_replace(' ', '+', $msg); // För base64...
		if ($msg == "quit") die();
		flush();
	}
}
?>